﻿#region Using Directives
using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Net;
using AuthorData.Entities;
using System.Linq;
using System.Collections.Generic;
#endregion

namespace AuthorData.Website
{
    public partial class _Default : System.Web.UI.Page
    {
        private string strUrl = "http://www.brainyquote.com/quotes/";
        string strUrlAssign = string.Empty;
        List<string> lstLink = new List<string>();
        List<Quotations> lstQuotations = new List<Quotations>();
        List<Authors> lstAuthor = new List<Authors>();
        List<Topics> lstTopics = new List<Topics>();
        List<Tags> lstTags = new List<Tags>();
        /// <summary>
        /// Handles the Load event of the Page class.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAuthorName(strUrl, "authors");
            }
        }
        private void GetAuthorName(string url, string match)
        {
            using (WebClient w = new WebClient())
            {
                string s = w.DownloadString(url);
                var query = LinkFinder.Find(s).AsEnumerable().OrderBy(LinkItem => LinkItem.Href);
                foreach (LinkFinder.LinkItem i in query)
                {
                    if (i.Href == null || string.IsNullOrEmpty(i.Href))
                        continue;
                    if (i.Href.Contains(strUrl + match) && i.Href.IndexOf("?") < 0 && !lstLink.Contains(i.Href))
                    {
                        lstLink.Add(i.Href);
                        ParseContentsValue(i.Href);
                    }
                }
            }
        }
        private void ParseContentsValue(string url)
        {
            using (WebClient w = new WebClient())
            {
                string s = w.DownloadString(url);
                LinkFinder.ParseMainContents(s, ref lstQuotations, ref lstAuthor, ref lstTopics, ref lstTags);
            }
        }
    }
}
